//package com.ctrip.corp.obt.shard.domain;
//
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.ctrip.corp.obt.generic.utils.CollectionUtils;
//import com.ctrip.corp.obt.generic.utils.StringUtils;
//import com.ctrip.corp.obt.shard.configuration.DruidDatabaseProperties;
//import com.ctrip.corp.obt.shard.constants.DbType;
//import com.ctrip.corp.obt.shard.exception.ShardException;
//import com.ctrip.corp.obt.shard.infrastructure.mapper.DataShardSourceDO;
//import com.ctrip.corp.obt.shard.utils.JdbcUtils;
//import lombok.Data;
//import lombok.experimental.Accessors;
//
//import javax.sql.DataSource;
//import java.util.Collections;
//import java.util.Date;
//import java.util.HashSet;
//import java.util.Map;
//import java.util.Objects;
//import java.util.Properties;
//import java.util.Set;
//
//import static com.ctrip.corp.obt.generic.constants.GenericConstants.DEFAULT;
//
///**
// * @author marco.zheng
// * @version 1.0
// * @date 2022/01/14
// * @desc 数据分片源信息
// * @see
// * @since 1.0
// */
//@Data
//@Accessors(chain = true)
//public class DataShardSource {
//
//    private DataSourceMeta dataSourceMeta;
//
//    /**
//     * 状态
//     */
//    private Integer status;
//
//    /**
//     * 数据库类型，默认为mysql
//     * {@link DbType}
//     */
//    private Integer dbType = DbType.MYSQL.getValue();
//
//    /**
//     * 数据库主从类型
//     */
//    private String masterSlaveType;
//
//    /**
//     * 分片参数名
//     */
//    private String tenantId;
//
////    /**
////     * 分片参数值
////     */
////    private String tenantIdColumn;
//
//    /**
//     * 分片模式（1、database 2、table）
//     * {@link com.ctrip.corp.obt.shard.constants.ShardMode}
//     */
//    private Integer shardMode;
//
//    /**
//     * 创建时间
//     */
//    private Date createTime;
//
//    /**
//     * 创建人名
//     */
//    private String createBy;
//
//    /**
//     * 更新时间
//     */
//    private Date updateTime;
//
//    /**
//     * 更新人名
//     */
//    private String updateBy;
//
//    /**
//     * 数据库连接属性json字符串
//     */
//    private String connectPropertiesJson;
//
//    /**
//     * 来源
//     */
//    private String source;
//
//    public static DataSource buildDefaultDatasource(DruidDatabaseProperties dbProperties) {
//        return new DataShardSource()
//                .setTenantId(DEFAULT)
////                .setCorpCode(DEFAULT)
//                .setDataSourceMeta(new DataSourceMeta()
//                        .setDriverClass(dbProperties.getDruidClassName())
//                        .setUrl(dbProperties.getUrl())
//                        .setUsername(dbProperties.getUsername())
//                        .setPassword(dbProperties.getPassword())
//                        .setInitialSize(dbProperties.getInitialSize())
//                        .setMaxActive(dbProperties.getMaxActive())
//                        .setMinIdle(dbProperties.getMinIdle())
//                        .setMaxWait(dbProperties.getMaxWait())
//                        .setTimeBetweenEvictionRunsMillis(dbProperties.getTimeBetweenEvictionRunsMillis())
//                        .setValidatingQuery(dbProperties.getValidatingQuery())
//                        .setTestWhileIdle(dbProperties.isTestWhileIdle()))
//                .setDbType(DbType.MYSQL.getValue())
//                .buildDataSource();
//    }
//
//    public DataSource buildDataSource() {
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUrl(this.dataSourceMeta.getUrl());
//        dataSource.setUsername(this.dataSourceMeta.getUsername());
//        try {
////            dataSource.setPassword(AESEncryptUtils.decrypt2Str(this.DataSourceMeta.getPassword()));
//            // TODO 后期的db密码都需要加密
//            dataSource.setPassword(dataSourceMeta.getPassword());
//        } catch (Exception ex) {
//            throw new ShardException(String.format("build datasource for tenant:%s password:%s decode error", this.getTenantId(), this.dataSourceMeta.getPassword()));
//        }
//        dataSource.setAsyncInit(true);
//        dataSource.setDriverClassName(this.dataSourceMeta.getDriverClass());
//        String props = this.getConnectPropertiesJson();
//        if (StringUtils.isNotBlank(props)) {
//            JSONObject jsonObject = JSON.parseObject(props);
//            Properties properties = new Properties();
//            for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {
//                properties.setProperty(String.valueOf(stringObjectEntry.getKey()),  String.valueOf(stringObjectEntry.getValue()));
//            }
//            dataSource.setConnectProperties(properties);
//        }
//        // 失败后重连的次数
//        dataSource.setConnectionErrorRetryAttempts(3);
//        // 请求失败之后中断
//        dataSource.setBreakAfterAcquireFailure(true);
//        try {
//            dataSource.init();
//            return dataSource;
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            throw new ShardException(this.getTenantId() + " build data source error " + ex.getMessage());
//        }
//    }
//
//    public static Set<DataShardSource> convertFrom(Set<DataShardSourceDO> dataShardSourceDOSet) {
//        if (CollectionUtils.isEmpty(dataShardSourceDOSet)) {
//            return Collections.emptySet();
//        }
//        Set<DataShardSource> dataShardSourceSet = new HashSet<>(dataShardSourceDOSet.size());
//        dataShardSourceDOSet.forEach(dataShardSourceDO -> dataShardSourceSet.add(convertFrom(dataShardSourceDO)));
//        return dataShardSourceSet;
//    }
//
//    private static DataShardSource convertFrom(DataShardSourceDO dataShardSourceDO) {
//        DbType dbType = JdbcUtils.getDbType(dataShardSourceDO.getUrl());
//        return new DataShardSource()
//                .setSource(dbType.getDb())
//                .setTenantId(dataShardSourceDO.getTenantId())
////                .setCorpCode(dataShardSourceDO.getCorpCode())
//                .setCreateTime(dataShardSourceDO.getCreateTime())
//                .setDataSourceMeta(new DataSourceMeta()
//                        .setDriverClass(dataShardSourceDO.getDriverClass())
//                        .setUrl(dataShardSourceDO.getUrl())
//                        .setUsername(dataShardSourceDO.getUsername())
//                        .setPassword(dataShardSourceDO.getPassword()))
//                .setDbType(dbType.getValue());
//    }
//
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (!(o instanceof DataShardSource)) return false;
//        DataShardSource that = (DataShardSource) o;
//        return Objects.equals(getDataSourceMeta(), that.getDataSourceMeta()) &&
////                Objects.equals(getCorpCode(), that.getCorpCode()) &&
//                Objects.equals(getStatus(), that.getStatus()) &&
//                Objects.equals(getDbType(), that.getDbType()) &&
//                Objects.equals(getMasterSlaveType(), that.getMasterSlaveType()) &&
//                Objects.equals(getTenantId(), that.getTenantId()) &&
//                Objects.equals(getShardMode(), that.getShardMode());
//    }
//
//    @Override
//    public int hashCode() {
//        return Objects.hash(getDataSourceMeta(), getStatus(), getDbType(), getMasterSlaveType(), getTenantId(), getShardMode());
//    }
//}
